import java.util.Scanner;

public class Demo24 {
    public static void main(String[] args) {
        Object locker = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (locker) {
                System.out.println("t1 wait 之前");
                try {
                    locker.wait(1000000000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1 wait 之后");
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (locker) {
                System.out.println("t2 wait 之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t2 wait 之后");
            }
        });
        Thread t3 = new Thread(() -> {
            synchronized (locker) {
                System.out.println("t3 wait 之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t3 wait 之后");
            }
        });

        Thread t4 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入任意内容, 唤醒线程");
            scanner.next();
            synchronized (locker) {
                // 当有多个线程等待时, notify 是唤醒随机的一个.
                // 此处的随机不是数学上的 "概率均等", 而是调度器有自己的规则, 我们感知不到, 干预不了~~
                locker.notifyAll();
            }
        });
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
